<?php
/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: mapi_base_control.php 3813 2014-01-27 02:55:07Z laoguozhang $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class stat_base_control extends dzf_base_control
{

	
	//统计在线时长标记变量 array('view' = array('idtype' => 'aId')) 
	//设置之后必须在上报扩展中增加对应处理函数和oper_type
	public $olcount = array();

	public function __construct() {
		parent::__construct();
	}
	/**
	 * 没有使用
	 */
	public function action_index() {
		$this->code = stat_base_msg::NO_ACTION;
		$this->echo_json();
	}
	
	/**
	 * 输出json数据信息
	 * @param Array $data
	 */
	public function echo_json($data = array()) {
		//#start
		$_akey = '#function#mapi_base_control::echo_json';
		DZF::analysisStart($_akey);
		//#end
		//ob_end_clean();
		header('Content-Type: text/html; charset=UTF-8');
		header("Content-type: application/json");
		
		$this->make_cors($_SERVER['REQUEST_METHOD'], 'http://open.wsq.qq.com');
		
		$_data = array('code' => $this->_code);
		
		if($this->_code) {
			$_data['msg'] = stat_base_msg::$CODES[$this->_code];
		} else {
			$_data['res'] = empty($data) ? (object)$this->data : array_merge($this->data, $data);
		}
		$_data['sys'] = array(
			//'channel' => $this->app->setting['channel'] ? (int)$this->app->setting['channel'] : 40,
			//'splash' => $this->app->setting['splash'] ? (int)$this->app->setting['splash'] : 40,
		);
		
		//执行时间大于1秒时记录
		if($_data['sys']['time'] > 0.9) {
			DZF::ext('log')->slow_request($this->request()->request_URI(), $_data['sys']['time']);
		}
			
		//#start
		DZF::analysisStop($_akey);
		//#end
		
		if($this->request()->get_string('debug')) {
			$_data['sys']['uin'] = DZF::app()->member['uin'];
			$_data['sys']['time'] = microtime(true) - STARTTIME;//执行时长，单位：秒
			$_data['sys']['memory'] = (round(memory_get_peak_usage() - STARTMEMORY) / 1048576 * 100) / 100;//使用内存，单位：MB
			//$_data['cmem'] = memory_driver_cmem::$stat;
			if($sqldebug = DB::$db->sqldebug) {
				foreach($sqldebug as $sqld) {
					$_data['sql'][] = $sqld[0];
				}
			}
			$_data['analysis'] = $_ENV['analysis'];
		}
		
		$jsdata = json_encode($_data);
		//添加对错误数据的过滤报警
//		if((TIMESTAMP + DZF::app()->member['uid']) % 10 === 0 && (strpos($jsdata, ':null') || strpos($jsdata, ':"Array"'))) {
//			DZF::ext('log')->dlog('8', 'has null or Array', 0);
//		}	
		echo $jsdata;
		exit;
	}
	
	/**
	 * 设置code
	 * @param string $code
	 */
	public function setcode($code) {
		if(isset(mapi_base_msg::$CODES[$code])) {
			$this->_code = $code;
		} else {
			$this->_code = mapi_base_msg::SYSTEM_ERROR;
		}
		
	}
	
	/**
	 * 在非control中需要显示信息且中断的地方使用
	 * @param string $code 错误代码
	 */
	static public function show_msg($code) {
		$ctl = new self();
		$ctl->code = $code;
		$ctl->echo_json();
	}
}
